﻿var myMask;
var LODOP = parent.PARENTLODOP;
function PrintMO(order_id) {
    //Ajax交互

    Ext.MessageBox.confirm("询问", '确定打印小单？', function(btn) {
        if (btn == "yes") {
            myMask = new Ext.LoadMask(Ext.getBody(), {
                msg: "正在获取数据，请稍候..."
            });
            myMask.show();
            var strUrl = strPath + 'GGFW/DocumentsManage/OrderCommand.fwf?Method=GetViewMO';
            Ext.Ajax.request({
                url: strUrl,
                method: 'POST',
                params: {
                    order_id: order_id
                    , start: 0
                    , limit: 500
                },
                failure: function(response, options) {
                    ReturnValue = Ext.MessageBox.alert("警告", "网络异常或登录过期，请重新登录！");
                    myMask.hide();
                },
                success: function(response, options) {
                    var json = response.responseText;
                    var obj = parseJsonResult(json);
                    if (!obj.Success) {
                        showMessage("错误！", "打印出错！请查看详细原因。", obj.Message);
                        myMask.hide();
                    }
                    else {
                        if (obj.totalCount < 1) {
                            showMessage("提示", "打印出错！没有数据！", "打印出错！没有数据！");
                            myMask.hide();
                            return;
                        }
                        GGFWMOPreview(obj.Records, obj.totalCount);
                    }
                }
            });
        }
    });
}

function GGFWMOPreview(Records, totalCount) {

    LODOP.PRINT_INIT("斯瑞泰_订单生产单打印_A4纵向");
    //LODOP.SET_PRINT_STYLE("FontSize", 10);
    var i = 0;

    var PoNo = Records[0].order_po;
    var SIZE = Records[0].code;

    for (n = 0; n < totalCount; n++) {

        result = ParseRecord(Records[n]);
        if (result == null)
            continue;

        for (j = 0; j < result.total; j++, i++) {
            if (j == 12) break; //预览只显示两页

            var k = i % 6;

            if (i > 0 && k == 0) {
                //直接打印
                //LODOP.PRINT();
                LODOP.NewPage();
            }

            var t = parseInt(k / 2) * 90;
            var l = (k % 2) * 95;
            t = t + ((k / 2 >= 1) ? 5.5 * parseInt(k / 2) : 0);
            l = l + ((k % 2 == 1 && k > 0) ? 5 : 0);

            /*头部*/
            var headT = 7 + t; //頭部高
            var headT_ = headT + 3.5; //頭部下划线高

            var titleL = 7 + l;  //标题起始点（横方向）
            var titleW = 8;  //标题宽度
            var lineW = titleW + 22.5;  //标题宽度+下划线宽度

            LODOP.ADD_PRINT_RECT(4 + t + "mm", 5 + l + "mm", "95mm", "90mm", 0, 1); //大框    ADD_PRINT_RECT(Top, Left, Width, Height,intLineStyle, intLineWidth)
            /******************頭部内容开始***********************/
            /*********第一列**************/
            LODOP.ADD_PRINT_TEXT(headT + "mm", titleL + "mm", "11mm", "3mm", "PoNo"); //PoNo   ADD_PRINT_TEXT(Top,Left,Width,Height,strContent)
            LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
            LODOP.ADD_PRINT_TEXT(headT + "mm", titleL + titleW + "mm", "24.5mm", "3mm", PoNo); //PoNo内容
            LODOP.ADD_PRINT_LINE(headT_ + "mm", titleL + titleW + "mm", headT_ + "mm", titleL + lineW + "mm", 0, 1); //下划线（PoNo内容）    (Top1, Left1, Top2, Left2, intLineStyle, intLineWidth)
            /*********第二列**************/
            LODOP.ADD_PRINT_TEXT(headT + "mm", titleL + lineW + "mm", "11mm", "3mm", "SIZE"); //SIZE（标题）
            LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
            LODOP.ADD_PRINT_TEXT(headT + "mm", titleL + lineW + titleW + "mm", "25mm", "6mm", SIZE); //SIZE内容
            LODOP.ADD_PRINT_LINE(headT_ + "mm", titleL + lineW + titleW + "mm", headT_ + "mm", titleL + 2 * lineW + "mm", 0, 1); //下划线（SIZE内容）
            /*********第三列**************/
            LODOP.ADD_PRINT_TEXT(headT + "mm", titleL + 2 * lineW + "mm", "11mm", "3mm", "色號"); //色號（标题）
            LODOP.ADD_PRINT_TEXT(headT + "mm", titleL + 2 * lineW + titleW - 1 + "mm", "24.5mm", "3mm", result.data[j].color); //色號内容
            LODOP.ADD_PRINT_LINE(headT_ + "mm", titleL + 2 * lineW + titleW - 1 + "mm", headT_ + "mm", titleL + 3 * lineW - 1 + "mm", 0, 1); //下划线（色號内容）
            /******************頭部内容結束***********************/

            /*左框*/
            var title1_T = 22 + t;   //标题起始点（竖直方向）
            var line1_T = 25.5 + t;  //下划线起始点（竖直方向）
            var interval1_T = 10;    //间隔（竖直方向）

            var begin_L1 = 8 + l;  //起始点1（横方向）
            var begin_L2 = 21 + l;  //起始点2（横方向）
            var begin_L3 = 15 + l;  //起始点3（横方向）

            var line_L = 52 + l;

            LODOP.ADD_PRINT_RECT(17 + t + "mm", 6 + l + "mm", "49mm", "56mm", 0, 1); //左框
            /******************左框内容开始***********************/
            /*********第一行**************/
            LODOP.ADD_PRINT_TEXT(title1_T + "mm", begin_L1 + "mm", "18mm", "3mm", "開料尺寸"); //開料尺寸（标题）
            LODOP.ADD_PRINT_TEXT(title1_T + "mm", begin_L2 + "mm", "32mm", "6mm", Records[n].kailiao_size); //開料尺寸内容
            LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
            LODOP.ADD_PRINT_LINE(line1_T + "mm", begin_L2 + "mm", line1_T + "mm", line_L + "mm", 0, 1); //下划线（開料尺寸内容）
            /*********第二行**************/
            LODOP.ADD_PRINT_TEXT(title1_T + interval1_T + "mm", begin_L1 + "mm", "11mm", "3mm", "重量"); //重量（标题）
            LODOP.ADD_PRINT_TEXT(title1_T + interval1_T + "mm", begin_L3 + "mm", "37mm", "3mm", result.data[j].weight); //重量内容
            LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
            LODOP.ADD_PRINT_LINE(line1_T + interval1_T + "mm", begin_L3 + "mm", line1_T + interval1_T + "mm", line_L + "mm", 0, 1); //下划线（重量内容）
            /*********第三行（空行）**************/
            LODOP.ADD_PRINT_TEXT(title1_T + 2 * interval1_T + "mm", begin_L1 + "mm", "44mm", "3mm", ""); //空行内容
            LODOP.ADD_PRINT_LINE(line1_T + 2 * interval1_T + "mm", begin_L1 + "mm", line1_T + 2 * interval1_T + "mm", line_L + "mm", 0, 1); //下划线（空行内容）
            /*********第四行**************/
            LODOP.ADD_PRINT_TEXT(title1_T + 3 * interval1_T + "mm", begin_L1 + "mm", "18mm", "3mm", "排發尺寸"); //排發尺寸（标题）
            LODOP.ADD_PRINT_TEXT(title1_T + 3 * interval1_T + "mm", begin_L2 + "mm", "32mm", "3mm", Records[n].paifa_pf_size); //排發尺寸内容
            LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
            LODOP.ADD_PRINT_LINE(line1_T + 3 * interval1_T + "mm", begin_L2 + "mm", line1_T + 3 * interval1_T + "mm", line_L + "mm", 0, 1); //下划线（排發尺寸内容）
            /*********第五行**************/
            LODOP.ADD_PRINT_TEXT(title1_T + 4 * interval1_T + "mm", begin_L1 + "mm", "11mm", "3mm", "尺碼"); //尺碼（标题）
            LODOP.ADD_PRINT_TEXT(title1_T + 4 * interval1_T + "mm", begin_L3 + "mm", "37mm", "3mm", "140"); //尺碼内容
            LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
            LODOP.ADD_PRINT_LINE(line1_T + 4 * interval1_T + "mm", begin_L3 + "mm", line1_T + 4 * interval1_T + "mm", line_L + "mm", 0, 1); //下划线（尺碼内容）
            /******************左框内容结束***********************/

            /*右框*/
            var title2_T = 22 + t;   //标题起始点（竖直方向）
            var line2_T = 25.5 + t;  //下划线起始点（竖直方向）
            var interval2_T = 10;    //间隔（竖直方向）

            var title2_L = 58 + l;   //标题起始点（横方向）
            var title2W = 7;  //标题宽度
            var line2W = title2W + 27.5; //标题宽度 + 下划线宽度

            LODOP.ADD_PRINT_RECT(17 + t + "mm", 56 + l + "mm", "42.8mm", "56mm", 0, 1); //右框
            /******************右框内容开始***********************/
            /*********第一行**************/
            LODOP.ADD_PRINT_TEXT(title2_T + "mm", title2_L + "mm", "11mm", "3mm", "磅發"); //磅發（标题）
            LODOP.ADD_PRINT_TEXT(title2_T + "mm", title2_L + line2W + 1 + "mm", "6mm", "3mm", "扎"); //扎（标题）
            LODOP.ADD_PRINT_TEXT(title2_T + "mm", title2_L + title2W + "mm", "28mm", "3mm", result.data[j].bangfa); //磅發内容
            LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
            LODOP.ADD_PRINT_LINE(line2_T + "mm", title2_L + title2W + "mm", line2_T + "mm", title2_L + line2W + "mm", 0, 1); //下划线（磅發内容）
            /*********第二行**************/
            LODOP.ADD_PRINT_TEXT(title2_T + interval2_T + "mm", title2_L + "mm", "11mm", "3mm", "每扎"); //每扎（标题）
            LODOP.ADD_PRINT_TEXT(title2_T + interval2_T + "mm", title2_L + line2W + 1 + "mm", "6mm", "3mm", "克"); //克（标题）
            LODOP.ADD_PRINT_TEXT(title2_T + interval2_T + "mm", title2_L + title2W + "mm", "28mm", "3mm", result.data[j].zai); //每扎内容
            LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
            LODOP.ADD_PRINT_LINE(line2_T + interval2_T + "mm", title2_L + title2W + "mm", line2_T + interval2_T + "mm", title2_L + line2W + "mm", 0, 1); //下划线（每扎内容）
            /*********第三行**************/
            LODOP.ADD_PRINT_TEXT(title2_T + 2 * interval2_T + "mm", title2_L + "mm", "11mm", "3mm", "卷筒"); //卷筒（标题）
            LODOP.ADD_PRINT_TEXT(title2_T + 2 * interval2_T + "mm", title2_L + line2W + 1 + "mm", "6mm", "3mm", "寸"); //寸（标题）
            LODOP.ADD_PRINT_TEXT(title2_T + 2 * interval2_T + "mm", title2_L + title2W + "mm", "28mm", "3mm", Records[n].juanfa_tong); //卷筒内容
            LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
            LODOP.ADD_PRINT_LINE(line2_T + 2 * interval2_T + "mm", title2_L + title2W + "mm", line2_T + 2 * interval2_T + "mm", title2_L + line2W + "mm", 0, 1); //下划线（卷筒内容）
            /*********第四行**************/
            LODOP.ADD_PRINT_TEXT(title2_T + 3 * interval2_T + "mm", title2_L + "mm", "11mm", "3mm", "條數"); //條數（标题）
            LODOP.ADD_PRINT_TEXT(title2_T + 3 * interval2_T + "mm", title2_L + line2W + 1 + "mm", "6mm", "3mm", "條"); //條（标题）
            LODOP.ADD_PRINT_TEXT(title2_T + 3 * interval2_T + "mm", title2_L + title2W + "mm", "28mm", "3mm", result.data[j].tiaoshu); //條數内容
            LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
            LODOP.ADD_PRINT_LINE(line2_T + 3 * interval2_T + "mm", title2_L + title2W + "mm", line2_T + 3 * interval2_T + "mm", title2_L + line2W + "mm", 0, 1); //下划线（條數内容）
            /*********第五行**************/
            LODOP.ADD_PRINT_TEXT(title2_T + 4 * interval2_T + "mm", title2_L + "mm", "11mm", "3mm", "方向"); //方向（标题）
            LODOP.ADD_PRINT_TEXT(title2_T + 4 * interval2_T + "mm", title2_L + title2W + 9 + "mm", "28mm", "3mm", Records[n].juanfa_direction); //方向内容
            LODOP.ADD_PRINT_LINE(line2_T + 4 * interval2_T + "mm", title2_L + title2W + "mm", line2_T + 4 * interval2_T + "mm", title2_L + line2W + "mm", 0, 1); //下划线（條數内容）
            /******************右框内容结束***********************/

            var remark = "";
            if (Records[n].paifa_remark != null)
                remark = Records[n].paifa_remark;
            if (Records[n].paifa_remark != null && Records[n].remark2 != null)
                remark = remark + '，';
            if (Records[n].remark2 != null)
                remark = remark + Records[n].remark2
            /******************底部内容开始***********************/
            LODOP.ADD_PRINT_TEXT(80 + t + "mm", 7 + l + "mm", "6mm", "11mm", "備註"); //備註（标题）
            LODOP.ADD_PRINT_TEXT(75 + t + "mm", 12 + l + "mm", "88mm", "15mm", remark); //備註內容（标题）
            /******************底部内容結束***********************/
        }
    }
    myMask.hide();
    //打印预览
    LODOP.PREVIEW();
}

/* 解析订单数据 */
/* 本函数不校验数据合法性，请慎用 */
function ParseRecord(record) {
    if (record.color_num == null || record.color_num == "")
        return null;

    var colors = record.color_num.split(',');
    if (colors.length == 0)
        return null;

    var str = '{"data":[';   /*定义json 实例*/
    var total = 0;
    var tmp_str;

    /* 以 排码尺码 来判断是否要打印*/
    for (i = 0; i < colors.length; i++) {
        if (record.paifa_pm_size1 != null && record.paifa_pm_size1 != "") {
            tmp_str = ParsePaifa(record, colors[i], record.paifa_pm_size1, record.kailiao_color_deep);
            if (tmp_str != null) {
                str += tmp_str + ",";
                total++;
            }
        }
        if (record.paifa_pm_size2 != null && record.paifa_pm_size2 != "") {
            tmp_str = ParsePaifa(record, colors[i], record.paifa_pm_size2, record.kailiao_color_middle);
            if (tmp_str != null) {
                str += tmp_str + ",";
                total++;
            }
        }
        if (record.paifa_pm_size3 != null && record.paifa_pm_size3 != "") {
            tmp_str = ParsePaifa(record, colors[i], record.paifa_pm_size3, record.kailiao_color_light);
            if (tmp_str != null) {
                str += tmp_str + ",";
                total++;
            }
        }
    }
    str = (str.length > 9 ? str.substring(0, str.lastIndexOf(',')) : str) + '], "total" : "' + total + '"}';
    var result = parseJsonResult(str);

    return result;

    //var abc= {"data":[{"color" : "1/2", "weight" : "2/4", "bangfa" : "0.014285714285714285", "zai" : "1680", "tiaoshu" : "2"},{"color" : "1/2", "weight" : "6", "bangfa" : "0.02857142857142857", "zai" : "210", "tiaoshu" : "4"}{"color" : "3/4", "weight" : "4/8", "bangfa" : "0.02857142857142857", "zai" : "1680", "tiaoshu" : "4"},{"color" : "3/4", "weight" : "12", "bangfa" : "0.05714285714285714", "zai" : "210", "tiaoshu" : "8"}{"color" : "4/6", "weight" : "6/12", "bangfa" : "0.04285714285714286", "zai" : "1680", "tiaoshu" : "6"},{"color" : "4/6", "weight" : "18", "bangfa" : "0.08571428571428571", "zai" : "210", "tiaoshu" : "12"}], "total" : "6"};
}

function ParsePaifa(record, color, paima, kailiao) {
    var int_weight;
    var str_weight;
    var left_kailiao;
    var right_kailiao;

    var arr1 = color.split('/');  /* 解析 "颜色/数量" */
    if (arr1.length != 2)
        return null;
    if (kailiao == "" || kailiao == null)
        return null;
    paima = isNaN(paima) ? 0 : parseFloat(paima);
    var shuliang = isNaN(arr1[1]) ? 0 : parseFloat(arr1[1]);
    var int_fatiao = isNaN(record.paifa_ft_size) ? 0 : parseFloat(record.paifa_ft_size);

    var arr2 = kailiao.split('/');  /* 解析 开料颜色，格式：a 或者 a/b */
    if (arr2.length == 1) {
        left_kailiao = isNaN(arr2[0]) ? 0 : parseFloat(arr2[0]);
        int_weight = left_kailiao * shuliang;
        str_weight = int_weight.toFixed(2).toString();
    } else if (arr2.length == 2) {
        left_kailiao = isNaN(arr2[0]) ? 0 : parseFloat(arr2[0]);
        right_kailiao = isNaN(arr2[1]) ? 0 : parseFloat(arr2[1]);
        int_weight = (left_kailiao + right_kailiao) * shuliang;
        str_weight = (left_kailiao * shuliang).toFixed(2).toString() + '/' + (right_kailiao * shuliang).toFixed(2).toString();
    }

    var bangfa = paima * shuliang / 140;
    var zai = int_weight / bangfa;
    var tiaoshu = int_fatiao == 0 ? "" : (paima * shuliang / int_fatiao).toString();

    var ret = '{"color" : "' + color + '", "weight" : "' + str_weight + '", "bangfa" : "' + bangfa.toFixed(2) + '", "zai" : "' + zai.toFixed(2) + '", "tiaoshu" : "' + tiaoshu + '"}';

    return ret;
}